<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/edit.css">
    <link rel="stylesheet" href="blog-editormd/css/editormd.css" />

</head>

<body>
    <div class="nav">
        <img src="pic/logo.jpg" alt="图片加载失败">
        <span class="blog-title">我的博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
        <a class="nav-span" href="#" onclick="logout()">注销</a>
    </div>
    <div class="content-edit">
        <div class="push">
            <input type="hidden" id="blogId">
            <input type="text" name="" id="title">
            <input type="button" value="更新文章" id="submit" onclick="submit()">
        </div>
        <!-- markdown 插件 html代码 -->
        <div id="editor">
            <textarea style="display:none;" id="content">##在这里写下一篇博客</textarea>
        </div>
    </div>

    <script src="js/jquery.min.js"></script>
    <script src="blog-editormd/editormd.min.js"></script>
    <script src="js/common.js"></script>
    <script type="text/javascript">

        let id = new URLSearchParams(window.location.search).get('id');

        $(function () {
            var editor = editormd("editor", {
                width  : "100%",
                height : "550px",
                path: "blog-editormd/lib/"
            });
        });
        
        function submit() {
           // 1、获取输入框的值，并构造JSON数据
            let title = $('#title').val();
            let content = $('#content').val();
            if (title == '' || content == '') { // 这里也可能判断是否为默认内容
                alert('请输入完整的文章信息');
                return;
            }
            let article = JSON.stringify({
                "articleId": id,
                "title": title,
                "content": content 
            });
            // 2、发送Ajax请求
            $.ajax({
                type: "put",
                url: "/blog/updateBlog",
                data: article,
                contentType: "application/json",
                success(result) {
                    if (result.data) {
                        alert("更新成功");
                        location.assign('./blog_list.html');
                    } else {
                        alert('更新失败');
                    }
                },
                error() {
                    alert('HTTP请求失败');
                }
            });
        }

        // 加载正在编辑的博客信息
        function getBlogInfo() {
            $.ajax({
                type: "get",
                url: "/blog/getBlogDetail",
                data: {
                    "articleId": id
                },
                success(result) {
                    if (result.data != null) {
                        $('#title').val(result.data.articleName);
                        $('#content').html(result.data.content);
                    }
                },
                error() {
                    alert('HTTP请求失败');
                }
            });
        }
        getBlogInfo();
    </script>
</body>

</html>